﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ToolsLayer;
using Webcar.BLL;
using System.Data;

namespace Webcar.SysManage
{
    public partial class Files : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridViewStyler gvs = new GridViewStyler(this.GridView1);
            gvs.SetGridHover = false;
            gvs.SetStyle();
            if (this.IsPostBack) return;
            this.SearchPanel1.BindColumn("files.xml");
            this.SearchPanel1.OriginSql = @"select a.*,b.name uploadman,c.base_name kind_name,CONVERT(varchar,filedate,120) as filedate_str
                from bs_files a,v_all_users b,bs_code c
                where a.oper_code = b.account and a.kind=c.base_code";

            this.BindData();
        }

        void BindData()
        {
            this.WebPager1.ItemsPerPage = 13; //每页显示的记录条数。
            string s_sort = this.SearchPanel1.GetSort();
            if (s_sort == "")
                s_sort = "filedate";
            this.WebPager1.SortField = s_sort; //用于排序的字段。
            this.WebPager1.Sortmode = DevCenterNew.SortMode.DESC;
            if (this.SearchPanel1.GetSortMode().ToUpper() == "DESC")
                this.WebPager1.Sortmode = DevCenterNew.SortMode.DESC;
            else if (this.SearchPanel1.GetSortMode().ToUpper() == "ASC")
                this.WebPager1.Sortmode = DevCenterNew.SortMode.ASC;
            this.WebPager1.ControlToPaginate = "GridView1"; //与其关联需绑定的DataGrid或DataList控件ID。

            this.WebPager1.SelectCommand = this.SearchPanel1.GetQuerySql();
            this.WebPager1.ConnectionString = DB.ConnectString;
            this.WebPager1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            HyperLink link = (HyperLink)e.Row.FindControl("HyperLink1");
            if (link == null) return;
            DataRowView row = e.Row.DataItem as DataRowView;
            link.NavigateUrl = "~/files/" + row["FileName"].ToString();
            if (CurrentUser.IsSuperUser() == false && row["oper_code"].ToString() != CurrentUser.LoginUserID)
            {
                (e.Row.FindControl("DeleteBtn") as LinkButton).Enabled = false ; 
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string sql = "delete from bs_files where fileid=" + e.CommandArgument;
                DB.ExecuteNonQuery(sql);
                this.BindData();
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
 
        }
    }
}
